//
//  XXTimer.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/3/31.
//

import Foundation
class XXTimer:Timer {
    
    public typealias XXTimerRepeatsBlock = (Int)->Bool
    
    /// 循环返回 数字
    /// - Parameters:
    ///   - count: 次数
    ///   - timeInterval: 间隔
    ///   - order: 正序 / 倒序
    ///   - block: 代码块  返回true继续  false停止
    class func repeats(count:Int = 60, _ timeInterval:TimeInterval = 1, order:Bool = false, _ block:@escaping XXTimerRepeatsBlock){
        var num = 1
        if !order {
            num = count
        }
        Timer.scheduledTimer(withTimeInterval:timeInterval, repeats: true) { timer in
            if !block(num){
                timer.invalidate()
            }else{
                if order {
                    if num == count{
                        timer.invalidate()
                    }else{
                        num += 1
                    }
                }else{
                    if num == 1{
                        timer.invalidate()
                    }else{
                        num -= 1
                    }
                }
            }
        }
    }
}
